<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class UserInfo extends AuthBase
{
    //获取user信息
    public function index()
    {
        $user = (new \app\common\business\UserInfo())->getInfoSkillLabelByField('user_id',$this->userId);
        return Show::success($user, "OK");
    }

    //更新user_info表信息
    public function update(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $education = input("education", 0, "intval");
        $experience = input("experience", 0, "intval");
        $intention = input("intention", 0, "intval");
        $free = input("free", 0, "intval");
        $skill_certificate = $this->request->param("skill_certificate", "", "trim");

        //参数校验
        $data = [
            'education' => $education,
            'experience' => $experience,
            'intention' => $intention,
            'free' => $free,
            'skill_certificate' => $skill_certificate,
        ];
        $validate = new \app\api\validate\UserInfo();
        if (!$validate->scene('update')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\UserInfo();
        try {
            $user = $userBisObj->updateData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }

    //更新user_info表信息 - 个人简介及技能标签
    public function updateLabel(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $skill_label = $this->request->param("skill_label", "", "trim");//技能标签id逗号分隔的字符串
        $introduce = $this->request->param("introduce", "", "trim");

        //参数校验
        $data = [
            'skill_label' => $skill_label,
            'introduce' => $introduce,
        ];
        $validate = new \app\api\validate\UserInfo();
        if (!$validate->scene('update_label')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\UserInfo();
        try {
            $user = $userBisObj->updateData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }
}
